//
//  ViewController.m
//  30207
//
//  Created by student on 16/4/7.
//  Copyright © 2016年 NIIT. All rights reserved.
//

#import "ViewController.h"
#import "DrawView.h"
#import "UIImage+ColorAtPixel.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet DrawView *drawview;

@property(weak,nonatomic)UIView *curselcolorview;
@property(nonatomic,weak)UIImageView *colorimageview;
@property(nonatomic,strong)UIView *selcolorview;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(UIView *)selcolorview
{
    if(_selcolorview==nil)
    {
        UIView *view=[[UIView alloc]initWithFrame:CGRectMake(0, 568, 320, 300)];
        UIView *bgview=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 300)];
        bgview.backgroundColor=[UIColor blackColor];
        bgview.alpha=0.5;
        [view addSubview:bgview];
        _selcolorview=view;
        
        
        UIView *curselcolorview=[[UIView alloc]initWithFrame:CGRectMake(5, 5, 50, 30)];
        curselcolorview.backgroundColor=[UIColor whiteColor];
        [view addSubview:curselcolorview];
        self.curselcolorview=curselcolorview;
        UIImageView *imageview=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"colorwheel"]];
        imageview.frame=CGRectMake(10, 0, 300, 300);
        [view addSubview:imageview];
        self.colorimageview=imageview;
        [self.view addSubview:view];
    }
    return _selcolorview;
}
- (IBAction)chexiao:(id)sender
{
    [self.drawview undo];
}
- (IBAction)qingping:(id)sender
{
    [self.drawview clear];
}
- (IBAction)xiangpica:(id)sender
{
    self.drawview.curcolor=[UIColor whiteColor];
}
- (IBAction)baocun:(id)sender {
}
- (IBAction)red:(id)sender
{
    self.drawview.curcolor=[UIColor redColor];
}
- (IBAction)yellow:(id)sender
{
    self.drawview.curcolor=[UIColor yellowColor];
}
- (IBAction)change:(id)sender
{
    [self showselcolorview];
}
- (IBAction)changed:(UISlider *)sender
{
    self.drawview.curlinrwidth=sender.value;
}
-(void)showselcolorview
{
    [UIView animateWithDuration:0.2 delay:0
                        options:UIViewAnimationOptionCurveEaseInOut animations:^{
                            self.selcolorview.frame = CGRectMake(0, 568-300, 320,300);
                        } completion:^(BOOL finished) {
                            
                        }];
}
-(void)hideselcolorview
{
    [UIView animateWithDuration:0.2 delay:0
                        options:UIViewAnimationOptionCurveEaseInOut animations:^{
                            self.selcolorview.frame = CGRectMake(0, 568, 320,300);
                        } completion:^(BOOL finished) {
                            
                        }];
}
-(void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    UITouch *touch=[touches anyObject];
    CGPoint point=[touch locationInView:self.colorimageview];
    
    UIImage *img=[UIImage imageNamed:@"colorwheel"];
    UIColor *selcolor=[img colorAtPixel:point];
    self.curselcolorview.backgroundColor=selcolor;
    self.drawview.curcolor=selcolor;
}
-(void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self hideselcolorview];
}
@end
